package event

import (
	"context"
	"ez/apps/erp/auto/mc"
	erp "ez/apps/erp/document"
	"ez/apps/tool/service"
	"ez/custom/cc"
	"gitee.com/dreamwood/ez-go/ez"
	"time"
)

func init() {

	ez.Subscribe(mc.SellFeedbackEventBeforeCreate, func(v interface{}, ctx context.Context) {
		md, ok := v.(*erp.SellFeedback)
		if ok {
			uid := cc.GetUidFromCtx(ctx)
			md.CreateEmpId = uid
			if md.FormEmpId == 0 {
				md.FormEmpId = uid
				md.FormAt = time.Now()
			}
			if md.Sn == "" {
				md.Sn = service.CreateSnByString(ez.ConfigService.AppId,
					"XSBJ-[YY][MM][DD]-[DMAX[3]]", 1)
			}
		}
	})
	ez.Subscribe(mc.SellFeedbackEventBeforeSave, func(v interface{}, ctx context.Context) {
		md, ok := v.(*erp.SellFeedback)
		if ok {
			//自动补充sellerName
			if md.BuyerId > 0 {
				md.LoadBuyer()
				if md.Buyer != nil {
					md.BuyerName = md.Buyer.Name
				}
			}
		}
	})
}
